﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;

namespace EthnicEdu.Pages
{
    public partial class QuestionDetailPage : PhoneApplicationPage
    {
        Service.IeThicEducationServiceClient service = new Service.IeThicEducationServiceClient();
        List<Service.Question> lstQuestion = new List<Service.Question>();


        int index = 0;
        public QuestionDetailPage()
        {
            InitializeComponent();
            service.GetAllWaitingQuestionAsync();
            
            service.GetAllWaitingQuestionCompleted += new EventHandler<Service.GetAllWaitingQuestionCompletedEventArgs>(service_GetAllWaitingQuestionCompleted);
            service.GetAllAnswerByQuestionIdCompleted += new EventHandler<Service.GetAllAnswerByQuestionIdCompletedEventArgs>(service_GetAllAnswerByQuestionIdCompleted);
            service.SetQuestionVerifyingCompleted += new EventHandler<Service.SetQuestionVerifyingCompletedEventArgs>(service_SetQuestionVerifyingCompleted);
            service.SetQuestionStateCompleted += new EventHandler<Service.SetQuestionStateCompletedEventArgs>(service_SetQuestionStateCompleted);
            
            
        }

        void service_SetQuestionStateCompleted(object sender, Service.SetQuestionStateCompletedEventArgs e)
        {
            lstQuestion[index].QuestionState = e.Result;
            if (index < lstQuestion.Count - 1)
            {
                index++;
                Binding();
            }
        }

        void btnNext_Click(object sender, EventArgs e)
        {

            if (index < lstQuestion.Count - 1)
            {

                index++;
                service.GetAllAnswerByQuestionIdAsync(lstQuestion[index].QuestionId);
                Binding(); 
            }
        }

        void btnDenine_Click(object sender, EventArgs e)
        {

            service.SetQuestionStateAsync(lstQuestion[index].QuestionId, 3);
            service.GetAllWaitingQuestionAsync();
        }

        void btnApprove_Click(object sender, EventArgs e)
        {
            service.SetQuestionStateAsync(lstQuestion[index].QuestionId, 1);
            
        }

        void service_GetAllAnswerByQuestionIdCompleted(object sender, Service.GetAllAnswerByQuestionIdCompletedEventArgs e)
        {
            try
            {
                lstQuestion[index].Answers = e.Result;
                Binding();
                service.SetQuestionVerifyingAsync(lstQuestion[index].QuestionId);
            }
            catch 
            {
                
            }
            
        }

        private void Binding()
        {
            try
            {
                if (lstQuestion.Count > 0)
                {
                    this.DataContext = lstQuestion[index];
                    this.lstAnswers.ItemsSource = lstQuestion[index].Answers;
                }
                else {
                    Service.Question question = new Service.Question();
                    question.Title = "Chưa có câu hỏi nào cần Approve !";
                    this.DataContext = lstQuestion[index];
                }
            }
            catch 
            {
                
            }
        }

        void service_SetQuestionVerifyingCompleted(object sender, Service.SetQuestionVerifyingCompletedEventArgs e)
        {
            lstQuestion[index].IsVerifying = e.Result;
            if (index < lstQuestion.Count - 1 && index != 0)
            {
                Binding();
            }
        }

        void service_GetAllWaitingQuestionCompleted(object sender, Service.GetAllWaitingQuestionCompletedEventArgs e)
        {

            try
            {
                index = 0;
                lstQuestion = new List<Service.Question>();
                lstQuestion = e.Result.ToList();
                service.GetAllAnswerByQuestionIdAsync(lstQuestion[index].QuestionId);
            }
            catch 
            {
                
                
            }
        }

        private void btnExchange_Click(object sender, EventArgs e)
        {
            index = 0;
            service.GetAllWaitingQuestionAsync();
        }

        
    }
}